package cn.tianjindong.blog_manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.pojo.BlogArticleComment;
import cn.tianjindong.blog_manager.service.CommentService;

@Controller
public class CommentController {
	@Autowired
	private CommentService commentService;
	
	@RequestMapping("/comment.html")
	public String toCommentPage(Model model, @RequestParam(defaultValue = "1") Integer currentPage,
			@RequestParam(defaultValue = "15") Integer countItem){
		PageView<BlogArticleComment> pageView = commentService.getCommentPageView(currentPage, countItem);
		model.addAttribute("pageView", pageView);
		return "/comment";
	}
	
	@RequestMapping("/comment/{commentId}.action")
	public @ResponseBody String loadCommentById(@PathVariable Long commentId){
		BlogArticleComment comment = commentService.getCommentById(commentId);
		return JSON.toJSONString(comment);
	}
	
	@RequestMapping("/comment/delete.action")
	public @ResponseBody String deleteCommentById(Long commentId){
		commentService.deleteCommentById(commentId);
		return "{\"status\":\"ok\"}";
	}
}
